package com.starbaba.log4jstatics.service.impl;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.SQLQuery;
import org.springframework.stereotype.Component;

import com.starbaba.core.hibernate.DAOFactory;
import com.starbaba.core.hibernate.HibernateDAO;
import com.starbaba.core.web.page.PageResponse;
import com.starbaba.log4jstatics.model.Log4jCountBean;
import com.starbaba.log4jstatics.model.Log4jTableVO;
import com.starbaba.log4jstatics.service.LogStatisticsService;

@Component
@SuppressWarnings({"rawtypes","unchecked"})
public class LogStatisticsServiceImpl implements LogStatisticsService {

	@Override
	public List<Log4jTableVO> getLogList(Map<String, Object> paramsMap, int start, int offset, PageResponse pageResponse) {
		List<Log4jTableVO> logList = new ArrayList<Log4jTableVO>();
		String where = "";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
		String projectName = (String) paramsMap.get("projectname");
		if (projectName != null) {

			if (paramsMap.containsKey("line")) {
				String line_rever = new StringBuffer((String) paramsMap.get("line")).reverse().toString();
				where += " line_rever = '" + line_rever + "' AND ";
			}
			
			if (paramsMap.containsKey("exception")) {
				String exception = (String) paramsMap.get("exception");
				where += " exception = '" + exception + "' AND ";
			}
			
			where += " projectname = '" + projectName + "'";
		} else {
			where = " 1 = 1 ";
		}
		
		HibernateDAO dao = DAOFactory.getInstance().createHibernateDAO();
		SQLQuery sqlQuery = dao.createSQLQuery_("select  exception, firststack, createdate, id from sourcedata.log4j log4j where status = 1 AND " + where + " order by createdate desc");
		sqlQuery.setFirstResult(start);
		sqlQuery.setMaxResults(offset);
		List<Object> list = sqlQuery.list();
		for (int i = 0; i < list.size(); i++) {
			Object[] resultSet = (Object[]) list.get(i);
			Log4jTableVO log4jTableVO = new Log4jTableVO();
			
			log4jTableVO.exception = (String) resultSet[0];
			log4jTableVO.firststack = (String) resultSet[1];
			Date createdate = (Date) resultSet[2];
			log4jTableVO.createDateString = simpleDateFormat.format(createdate);
			log4jTableVO.id = ((BigInteger) resultSet[3]).longValue();
			
			logList.add(log4jTableVO);
		}
		
		SQLQuery countQuery = dao.createSQLQuery_("select count(1) from sourcedata.log4j where status = 1 AND " + where);
		Object countObject = countQuery.uniqueResult();
		if (countObject instanceof BigInteger) {
			int count = ((BigInteger) countObject).intValue();

			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		}
		return logList;
	}

	@Override
	public List<String> getProjectNameList() {
		List<String> result = new ArrayList<String>();
		String sql = "select DISTINCT(projectname) from sourcedata.log4j";
		HibernateDAO dao = DAOFactory.getInstance().createHibernateDAO();
		SQLQuery sqlQuery = dao.createSQLQuery_(sql);
		List<Object> list = sqlQuery.list();
		for (int i = 0; i < list.size(); i++) {
			result.add((String) list.get(i));
		}
		return result;
	}

	@Override
	public Log4jTableVO getLogDetail(Long id) {
		Log4jTableVO bean = new Log4jTableVO();
		HibernateDAO dao = DAOFactory.getInstance().createHibernateDAO();
		SQLQuery sqlQuery = dao.createSQLQuery_("select msg, stacktrace from sourcedata.log4j where id = " + id);
		List<Object> list = sqlQuery.list();
		for (int i = 0; i < list.size(); i++) {
			Object[] resultSet = (Object[]) list.get(i);
			bean.msg = (String) resultSet[0];
			bean.stacktrace = (String) resultSet[1];
		}
		return bean;
	}

	@Override
	public List<Log4jCountBean> getLogCountBeanList(Map<String, Object> paramsMap, int iDisplayStart, int iDisplayLength, PageResponse pageResponse) {
		List<Log4jCountBean> logList = new ArrayList<Log4jCountBean>();
		String projectName = (String) paramsMap.get("projectname");
		String where = null;
		if (projectName != null) {
			where = " projectname = '" + projectName + "'";
		} else {
			where = " 1 = 1 ";
		}
		
		
		HibernateDAO dao = DAOFactory.getInstance().createHibernateDAO();
		SQLQuery sqlQuery = dao.createSQLQuery_("select  line_rever, exception, count(1) c from sourcedata.log4j log4j where status = 1 AND " + where + " group by line_rever, exception order by c desc");
		sqlQuery.setFirstResult(iDisplayStart);
		sqlQuery.setMaxResults(iDisplayLength);
		List<Object> list = sqlQuery.list();
		StringBuffer sb = new StringBuffer();
		
		for (int i = 0; i < list.size(); i++) {
			Object[] resultSet = (Object[]) list.get(i);
			sb.delete(0, sb.length());
			
			Log4jCountBean log4jCountBean = new Log4jCountBean();
			log4jCountBean.exception = (String) resultSet[1];
			log4jCountBean.line = sb.append((String) resultSet[0]).reverse().toString();
			log4jCountBean.count = ((BigInteger) resultSet[2]).intValue();
			logList.add(log4jCountBean);
		}
		
		SQLQuery countQuery = dao.createSQLQuery_("select count(1) from (select count(1) from sourcedata.log4j where status = 1 AND " + where + " group by line_rever, exception) ss");
		Object countObject = countQuery.uniqueResult();
		if (countObject instanceof BigInteger) {
			int count = ((BigInteger) countObject).intValue();

			pageResponse.setITotalRecords(count);
			pageResponse.setITotalDisplayRecords(count);
		}
		
		return logList;
	}

	@Override
	public void setAsDone(Map<String, Object> paramsMap) {
		HibernateDAO dao = DAOFactory.getInstance().createHibernateDAO();
		StringBuffer sb = new StringBuffer();
		String line = (String) paramsMap.get("line");
		
		int record = dao.executeSQLUpdate("update sourcedata.log4j set status = 0 where exception = ? and line_rever = ? and projectname = ? and status = 1",
				paramsMap.get("exception"), sb.append(line).reverse().toString(), paramsMap.get("projectname"));
		System.out.println("setAsDone:" + record);
	}
	
}
